﻿using System.Collections.Generic;
using System.ComponentModel;
using YKXiangQi.Common;
using YKXiangQi.Enums;
using YKXiangQi.Models;
using YKXiangQi.Utilities;

namespace YKXiangQi.ViewModels
{
    public class EngineDetailsViewModel: DialogViewModel
    {
        private IDialogService DialogService { get; set; }

        public RelayCommand CancelCommand { get; set; }
        public RelayCommand SaveCommand { get; set; }
        public RelayCommand BrowseForFileCommand { get; set; }

        public IList<EnumMapper<EngineProtocol>> Protocols
        {
            get { return EnumHelper.LoadEnums<EngineProtocol>(); }
        }

        public Engine Engine { get;private set; }

        public EngineDetailsViewModel(Engine engine)
        {
            SaveCommand = new RelayCommand(OnSave);
            CancelCommand = new RelayCommand(OnCancel);
            BrowseForFileCommand = new RelayCommand(BrowseForFile);
            DialogService = new DialogService();

            Engine = engine;
        }

        public string Name
        {
            get { return Engine.Settings.Name; }
            set
            {
                Engine.Settings.Name = value;
                RaisePropertyChanged(() => this.Name);
            }
        }

        public int DepthLimit
        {
            get { return Engine.Settings.DepthLimit; }
            set
            {
                Engine.Settings.DepthLimit = value;
                RaisePropertyChanged(() => this.DepthLimit);
            }
        }

        public int TimeLimit
        {
            get { return Engine.Settings.TimeLimit; }
            set
            {
                Engine.Settings.TimeLimit = value;
                RaisePropertyChanged(() => this.TimeLimit);
            }
        }

        public string Path
        {
            get { return Engine.Settings.Path; }
            set
            {
                Engine.Settings.Path = value;
                RaisePropertyChanged(() => this.Path);
            }
        }

        public EngineProtocol Protocol
        {
            get { return Engine.Settings.Protocol; }
            set { Engine.Settings.Protocol = value; }
        }

        private void OnSave(object param)
        {
            if(string.IsNullOrEmpty(Name))
            {
                DialogService.ShowMessageBox("Please enter the engine's name.");
                return;
            }

            OnRequestClose(true);
        }

        private void OnCancel(object param)
        {
            OnRequestClose(false);
        }

        private void BrowseForFile(object param)
        {
            var fileName = DialogService.BrowseForFile("*.Exe file|*.exe");
            Path = fileName;
        }

        public string Error
        {
            get { return string.Empty; }
        }

        public string this[string columnName]
        {
            get
            {
                string result = null;
                if (columnName == "Name")
                    if (string.IsNullOrEmpty(Name))
                        result = "Please enter the engine's name.";
                
                return result;
            }
        }
    }
}
